package com.kefu.chat.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.basic.mapper.BasicTagMapper;
import com.kefu.basic.model.BasicTag;
import com.kefu.chat.model.ChatSession;
import com.kefu.chat.model.ChatSessionMsg;
import com.kefu.chat.model.ChatSessionTagRel;
import com.kefu.chat.service.ChatSessionMsgService;
import com.kefu.chat.service.ChatSessionService;
import com.kefu.chat.service.ChatSessionTagRelService;
import com.kefu.chat.vo.ChatSessionSearchVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.StreamUtil;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags="会话纪录")
@RestController
@RequestMapping("/chat/session/")
public class ChatSessionController extends AbstractBaseController{
    @Resource
    private ChatSessionMsgService chatSessionMsgService;
    @Resource
    private BasicTagMapper basicTagMapper;
    @Resource
    private ChatSessionService chatSessionService;
    @Resource
    private ChatSessionTagRelService chatSessionTagRelService;

    @PostMapping("/detail")
    @ApiOperation(value="会话详情", notes="会话详情")
    public Result<ChatSession> detail(@Validated @RequestBody DetailVo detailVo) {
        ChatSession info = chatSessionService.findById(detailVo.getId());
        List<ChatSessionTagRel> list = chatSessionTagRelService.list(Wrappers.lambdaQuery(ChatSessionTagRel.class)
                .eq(ChatSessionTagRel::getSessionId, detailVo.getId()));
        if (CollUtil.isNotEmpty(list)) {
            List<BasicTag> basicTags = basicTagMapper.selectList(Wrappers.lambdaQuery(BasicTag.class)
                    .in(BasicTag::getTagId, list.stream().map(ChatSessionTagRel::getTagId).collect(Collectors.toList())));
            info.setBasicTag(basicTags);
        }
        return ResultGenerator.genSuccessResult(info);
    }

    @PostMapping("/list")
    @ApiOperation(value = "会话列表", notes = "会话列表")
    public Result<Page<ChatSession>> sessionRecord(@Validated @RequestBody ChatSessionSearchVo search) {
        Page<ChatSession> page = new Page<>(search.getPageNo(), search.getPageSize(), true);
        List<String> sessionIds = Lists.newArrayList();
        if (StrUtil.isNotBlank(search.getMsgContent())) {
            List<ChatSessionMsg> list = chatSessionMsgService.list(Wrappers.lambdaQuery(ChatSessionMsg.class)
                    .like(ChatSessionMsg::getMsgContent, search.getMsgContent()));
            if (CollUtil.isEmpty(list)) {
                return ResultGenerator.genSuccessResult(page);
            }
            sessionIds.addAll(StreamUtil.stream(list).map(ChatSessionMsg::getSessionId).collect(Collectors.toList()));
        }
        if (CollUtil.isNotEmpty(search.getTagList())) {
            List<ChatSessionTagRel> list = chatSessionTagRelService.list(Wrappers.lambdaQuery(ChatSessionTagRel.class)
                    .in(ChatSessionTagRel::getTagId, search.getTagList()));
            if (CollUtil.isEmpty(list)) {
                return ResultGenerator.genSuccessResult(page);
            }
            sessionIds.addAll(StreamUtil.stream(list).map(ChatSessionTagRel::getSessionId).collect(Collectors.toList()));
        }
        Page<ChatSession> sessionList = chatSessionService.getSessionList(search);
        return ResultGenerator.genSuccessResult(sessionList);
    }
}


